/**
 * Created by Panda on 16/5/3
 */
(function () {
    'use strict';

    angular
        .module('com.akxx.aqzyk')
        .service('Storage', Storage);


    /** @ngInject */
    function Storage() {
        var self = this;

        self.getUser = function () {
            return self.get("user");
        };

        self.get = function (key, isString) {
            var res = window.sessionStorage[key];
            if (!!res && !isString) {
                res = angular.fromJson(res);
            }
            return res;
        };

        self.set = function (key, value) {
            self.remove(key);
            if(!!value){
                if (_.isObject(value)) {
                    value = angular.toJson(value);
                }
                window.sessionStorage[key] = value || "";
            }
        };

        self.remove = function (key) {
            window.sessionStorage.removeItem(key);
        };

        self.clear = function () {
            window.sessionStorage.clear();
        };

        /*删除除了user之外的所有缓存*/
        self.safeClear = function () {
            var user = self.get("user");
            self.clear();
            self.set("user",user);
        };

        self.setProperty = function (object, property, value) {
            var obj = self.get(object, true);
            obj[property] = value;
            self.set(object, obj);
        };

    }

})();
